package engine.newUi
{
	import engine.data.ModelLocator;
	import engine.data.View;
	import engine.data.ViewObject;
	import engine.flag.GameDefine;
	import engine.flag.ResourcesPath;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.ui.AlertUI;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.utils.setTimeout;
	
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.group.NxViewGroup;
	import newx.com.control.text.NxLabel;
	import newx.com.control.window.NxWindowEvent;
	import newx.utils.MemoryOptimise;

	
	/**
	 * 物品合成逻辑 
	 * @author liuel
	 * 
	 */	
	public final class ComposeItemLogic
	{
		protected const COLOR_TABLE	:Array	=	["","#ffffff'>","#1eff00'>","#0070dd'>","#a335ee'>","#ff8000'>","#e5cc80'>","#ffff00'>","#ff0000'>"];
		private static var _self	:ComposeItemLogic;
		private var _ui			:ComposeItemUI;
		private var _itemVie		:ViewObject;
		private var _itemMC		:MovieClip;
		private var _img			:NxClip;
		private var _txtCount		:NxLabel;
		//合成物品的ID
		private var _itemConfigID	:int;
		//合成物物品数量
		private var _itemCount		:int;
		//材料数组
		private var _materialArr	:Array;
		//材料的种类
		private var _materialCount	:int;
		private var _modle			:ModelLocator;
		//背包仓库中有对应材料数量的数组
		private var _haveCoutArr	:Array;
		private var _viewObj		:ViewObject;
		private var _type			:int;
		private var _vieType		:int;
		// 0 代表 从背包中点击转轴  不是0代表从装备上点击（人物属性）
		private var _state			:int;
		//合成的类型  如果为1侧从背包中点击转轴制造 2侧从装备栏点击
		private var _composeType	:int;
		
		//需要升级的物品
		private var _upgradeItem	:ViewObject;
		//生成的物品
		public var outputItem	    :Object;
		/**
		 *材料物品所在的位置 
		 */		
		private var _pos			:int=0;
		
		private var _resectMc:MovieClip;
		/**需要的元宝**/
		private  var _needMagicCoin:int=0;
		public static function getInstance():ComposeItemLogic
		{
			if(_self == null)
			{
				_self = new ComposeItemLogic();
			}
			return _self;
		}
		public function ComposeItemLogic()
		{
		}
		public function Init():void
		{
			_modle	=ModelLocator.getInstance();
			_ui		=ComposeItemUI.getInstance();
			initGroup(_ui.itemGroup);
			initGroup(_ui.materialGroup);
			_modle.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SCROLL_PRODUCTION_SUCCEES),getSuccees);
			_modle.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_AUTO_BUY_MATRIAL),getConsume)	
			_ui.succsemc.mouseChildren=false;
		}
		public function BeforeShow():void
		{
			
			sendConsumeMsg();
			_pos=0;
			setCompleBtnState();
			addEvent();
			updateBtn();
		}
		public function AfterHide():void
		{
			clearTeamGroup(_ui.materialGroup);
			clearTeamGroup(_ui.itemGroup);
			removeEvent();
			
		}
		private function addEvent():void
		{
		  _ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,onClose);
		  _ui.startBtn.addEventListener(MouseEvent.CLICK,onCompose);
		  _ui.materialGroup.addItemsEvent(MouseEvent.CLICK,onClick);
		  _ui.itemGroup.addItemsEvent(MouseEvent.MOUSE_OVER,onShowTips);
		  _ui.itemGroup.addItemsEvent(MouseEvent.MOUSE_OUT,onHideTips);
		  _ui.materialGroup.addItemsEvent(MouseEvent.MOUSE_OVER,onShowTips);
		  _ui.materialGroup.addItemsEvent(MouseEvent.MOUSE_OUT,onHideTips);
		  
		  _ui.compleBtn.addEventListener(MouseEvent.MOUSE_OVER,onshow);
		  _ui.compleBtn.addEventListener(MouseEvent.MOUSE_OUT,onHideTips);
		  _ui.compleBtn.addEventListener(MouseEvent.CLICK,onClickCompleHandle);
		}
		private function removeEvent():void
		{
		  _ui.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,onClose);
		  _ui.startBtn.removeEventListener(MouseEvent.CLICK,onCompose);
		  _ui.materialGroup.removeEventListener(MouseEvent.CLICK,onClick);
		  _ui.itemGroup.removeItemsEvent(MouseEvent.MOUSE_OVER,onShowTips);
		  _ui.itemGroup.removeItemsEvent(MouseEvent.MOUSE_OUT,onHideTips);
		  _ui.materialGroup.removeItemsEvent(MouseEvent.MOUSE_OVER,onShowTips);
		  _ui.materialGroup.removeItemsEvent(MouseEvent.MOUSE_OUT,onHideTips);
		  _ui.compleBtn.removeEventListener(MouseEvent.MOUSE_OVER,onshow);
		  _ui.compleBtn.removeEventListener(MouseEvent.MOUSE_OUT,onHideTips);
		  _ui.compleBtn.removeEventListener(MouseEvent.CLICK,onClickCompleHandle);
		}
		/**
		 *更新立即制作按钮状态 
		 * 
		 */		
		private function setCompleBtnState():void
		{
			_ui.compleBtn.enabled =_ui.startBtn.enabled=true;
			_ui.tip.htmlText =StaticIni.getSwfLangStr("UIString_1000459");
			var vipLevel:int = _modle.scene.Role.getVipLevel();
			if(vipLevel>=4)
			{
		
				_ui.startBtn.x = 362;
				_ui.compleBtn.visible= true;
		
			}
			else
			{
				_ui.startBtn.x = 403;
				_ui.compleBtn.visible = false;
			}
//			if(haveEquip())
//			{
//				_ui.compleBtn.enabled=true;
//			}
//			else
//			{
//				_ui.compleBtn.enabled=false;
//			}
		}
		private function haveEquip():Boolean
		{
			var obj:Object = StaticIni.getIniObj(_materialArr[0]);
			if(!obj)
				return true;
			if(_composeType ==1 &&_haveCoutArr[0]<1 &&obj.hasOwnProperty("EquipType"))
			{
				return false;
			}
			return true;
		}
		private function initGroup(group:NxViewGroup):void
		{
			
			for (var i : int = 0; i <  group.row*group.col; i++)
			{
				_txtCount   =	new NxLabel();
				_itemMC	=	group.getItemByIndex(i);
				MemoryOptimise.stopMovie(_itemMC);
				_img		=	new NxClip(); 
				_img.defaultImage   =	null;
				_img.isCache		=	true;
				_img.source         =   "";
				if(group == _ui.itemGroup)
				{
				   _img.x =12;
				   _img.y =11;
				   _txtCount.x=10;
				   _txtCount.y=10;
				}
				else
				{
					_img.x =4;
					_img.y =4;
				}
				_itemMC["img"]     =   _img;
				_itemMC.addChild(_img);
				_itemMC["txtCount"] = _txtCount;
				_itemMC.addChild(_txtCount);
		
			}
		}
		private function updateBtn():void
		{
			if(checkIsCan())
			{
				_ui.startBtn.enabled	= true;
			}
			else
			{
				_ui.startBtn.enabled	= false;
			}
		}
		/**
		 * 设置根据点击的物品时信息 设置数据 
		 * @param itemObj     物品
		 * @param type        1从背包点击  2从装备点击
		 * @param viewType    物品所在视图
		 * @param okey		  物品所在视图位置   如果点击的是卷轴测调用时传0
		 * 
		 */		
		public function SetItem(itemObj:ViewObject,type:int,viewType:int,okey:int):void
		{
			
			_viewObj =null;
			if(itemObj == null)
				return;
			_composeType= type;
			_viewObj = itemObj;
			_state 	=okey;
			
			_type		=	type;
			_vieType	=	viewType;
			readItemIni(itemObj);
			readIniMaterial(itemObj);
			updataItem([_itemConfigID,_itemCount],_ui.itemGroup);
			searchALL();
			updataItem(_materialArr,_ui.materialGroup);
			if(_ui.visible == false)
			{
				_ui.succsemc.visible=false;
				_ui.show();
			}
		}
		/**
		 * 读取配置文件合成物品
		 * @param itemObj
		 * 
		 */		
		public function readItemIni(itemObj:ViewObject):Object
		{
			if(itemObj)
			{
			  var obj:Object=StaticIni.getIniObj(itemObj.GetPropertiesEx("ConfigID"));
              if(obj == null)
				 return null;
			  _itemConfigID =obj["GetItem"];
			  _itemCount	  =obj["GetItemNum"];
			}
			return obj;
		}
		/**
		 *读取配置文件合成需要的材料 
		 * 
		 */		
		private function readIniMaterial(itemObj:ViewObject):void
		{
			_materialArr=[];
			var obj:Object=StaticIni.getIniObj("ProduceItem_"+itemObj.GetPropertiesEx("ConfigID"));
			if(obj == null)
				return;
			var str:String=obj["r"];
			
			var arr:Array=[];
			arr=str.split(",");
			for each (var i:String in arr) 
			{
				if(i!="")
				_materialArr.push(i);
			}
			_materialCount=_materialArr.length/2;
		   
		}
		/**
		 * 更新信息 
		 * @param index 物品所在数组索引
		 * @param item
		 * 
		 */		
		private function updataItem(arr:Array,group:NxViewGroup):void
		{
			for(var i : int = 0; i < group.row*group.col; i ++)
			{
				updataItemGroup1(i, arr,group);
			}
			
		}
		/**
		 * 设置点击升级的物品 （仅用于从装备栏点击时使用） 
		 * @param val
		 * 
		 */		
		public function set UpgradeItem(val:ViewObject):void
		{
			_upgradeItem = val;
		}
		public function get UpgradeItem():ViewObject
		{
			return _upgradeItem;
		}
		private function updataItemGroup1(index:int,arr:Array,group:NxViewGroup):void
		{
			_itemMC= group.getItemByIndex(index);
			if(_itemMC == null)return;
			var count:int;
			group == _ui.itemGroup?count = _itemCount:count = _materialCount;
	
			if(group == _ui.itemGroup) //合成物品
			{
				var name:String="";
				name = StaticIni.getSwfLangStr(arr[index*2]);
				if(_composeType == 2 && index == 0 )
				{
					var vo:ViewObject =_upgradeItem; //原始装备
					var color:int=StaticIni.getIniObj(vo.GetPropertiesEx("UpgradeScroll")).Color;
					var lv:int=vo.GetProperties("IntensityLevel"); //原强化等级
					var currLv:int=0;
					if(lv>5)
					{
						currLv=lv-5
					}
					else
					{
						currLv=0;
					}
					var obj:Object=StaticIni.getIniObj(arr[index*2]);
					if(obj == null)
						return;
					(_itemMC["img"] as NxClip).source            =  ResourcesPath.getPropsURL(int(obj["Photo"]));
					_itemMC["date"]=EquipModifiedLogic.getInstance().getCurrLvItemObject(arr[index*2],currLv,color);
				}
				else
				{
			
					var obj1:Object=StaticIni.getIniObj(arr[index*2]);
					 if(obj1 ==null)return; 
					if(obj1.hasOwnProperty("EquipType"))
					{
						_upgradeItem=searchItem(_materialArr[0],2);
						var vo1:ViewObject =_upgradeItem; //原始装备
						if(vo1==null)
						{
							if(obj1 == null)return;
							obj1["ConfigID"]=arr[index*2];
							(_itemMC["img"] as NxClip).source            =  ResourcesPath.getPropsURL(int(obj1["Photo"]));
							val>=val1?_itemMC["txtCount"].htmlStr =link_color(val+"/"+val1,2):_itemMC["txtCount"].htmlStr =link_color(val+"/"+val1,1);
							_itemMC["date"] = obj1;
							_itemMC["txtCount"].text =arr[index*2+1]+"";
							_ui.targetlabel.htmlText = StaticIni.getSwfLangStrVar("UIString_1000458",[name])
							return;
						}
						var color1:int=StaticIni.getIniObj(vo1.GetPropertiesEx("UpgradeScroll")).Color;
						var lv1:int=vo1.GetProperties("IntensityLevel"); //原强化等级
						var currLv1:int=0;
						if(lv1>5)
						{
							currLv1=lv1-5
						}
						else
						{
							currLv1=0;
						}
						var obj2:Object=StaticIni.getIniObj(arr[index*2]);
						if(obj2 == null)
							return;
						(_itemMC["img"] as NxClip).source            =  ResourcesPath.getPropsURL(int(obj2["Photo"]));
						
						_itemMC["date"]=EquipModifiedLogic.getInstance().getCurrLvItemObject(arr[index*2],currLv1,color1);
					}
					else
					{
						if(obj1 == null)return;
						obj1["ConfigID"]=arr[index*2];
						(_itemMC["img"] as NxClip).source            =  ResourcesPath.getPropsURL(int(obj1["Photo"]));
						val>=val1?_itemMC["txtCount"].htmlStr =link_color(val+"/"+val1,2):_itemMC["txtCount"].htmlStr =link_color(val+"/"+val1,1);
						_itemMC["date"] = obj1;
					}
					
				}
				_itemMC["txtCount"].text =arr[index*2+1]+"";
				_ui.targetlabel.htmlText = StaticIni.getSwfLangStrVar("UIString_1000458",[name])
			}
			else  //材料
			{
				if(index>=_materialCount)
				{
					_itemMC.visible=false;
					return;
				}
				else
				{
					_itemMC.visible=true;
				}
				var val	:int	=_haveCoutArr[index];
				var val1:int	=arr[index*2+1];
				if(_composeType == 2 && index == 0 )//当从人物属性点击时候第一个材料不需要读取配置文件
				{
					var vo2:ViewObject =_upgradeItem;
					if(vo2==null)return;
					(_itemMC["img"] as NxClip).source			=  ResourcesPath.getPropsURL(vo2.GetProperties("Photo"));
					var currnum:int =searchItem1(vo2)+1;
					currnum>=val1?_itemMC["titleTxt2"].htmlText	=  link_color(currnum+"/"+val1,2):_itemMC["titleTxt2"].htmlText =link_color(currnum+"/"+val1,1);
					_itemMC["date"] = vo2;
				}
				else  //普通合成
				{
				
					var obj3:Object=StaticIni.getIniObj(arr[index*2]);
					if(obj3 == null)return;
					if(obj3.hasOwnProperty("EquipType"))
					{
						var vo3:ViewObject =searchItem(arr[index*2],2);
						if(vo3==null)
						{
							obj3["ConfigID"]=arr[index*2];
							(_itemMC["img"] as NxClip).source            =  ResourcesPath.getPropsURL(int(obj3["Photo"]));
							val>=val1?(_itemMC["titleTxt2"]as TextField).htmlText =link_color(val+"/"+val1,2):_itemMC["titleTxt2"].htmlText =link_color(val+"/"+val1,1);
							_itemMC["date"] = obj3;
							_pos=0;
							return
						}
						_pos=int(vo3.ObjKey);
						(_itemMC["img"] as NxClip).source			=  ResourcesPath.getPropsURL(vo3.GetProperties("Photo"));
						var currNum:int=searchItem1(vo3);
						currNum=_composeType ==1?currNum:currNum+1   //如果从装备栏中点击的测现有数+1
						_itemMC["titleTxt2"].htmlText=currNum>=val1?link_color(currNum+"/"+val1,2):link_color(currNum+"/"+val1,1);
						_itemMC["date"] = vo3;
					}
					else
					{
						
						obj3["ConfigID"]=arr[index*2];
						(_itemMC["img"] as NxClip).source            =  ResourcesPath.getPropsURL(int(obj3["Photo"]));
						val>=val1?_itemMC["titleTxt2"].htmlText =link_color(val+"/"+val1,2):_itemMC["titleTxt2"].htmlText =link_color(val+"/"+val1,1);
						_itemMC["date"] = obj3;
					}
					
					
				}
			}
		}
		/**
		 * 更新一个物品信息 
		 * @param index
		 * @param arr
		 * 
		 */		
		private function updataItemGroup(index : int, arr : Array,group:NxViewGroup):void
		{
			_itemMC	=	group.getItemByIndex(index);
			if(_itemMC != null)
			{	
				var count:int;
				group == _ui.itemGroup?count = _itemCount:count = _materialCount;
				if(index >= count)
				{
					_itemMC.visible = false;
					return;
				}
				_itemMC.visible    = true;
				//当从装备点击时候 材料第一个位置为该装备 不需要读取配置文件
				if(_composeType == 2 && index == 0 && group == _ui.materialGroup)
				{
					var vo:ViewObject =_upgradeItem;
					if(vo ==null)
						return;
					(_itemMC["img"] as NxClip).source            =  ResourcesPath.getPropsURL(vo.GetProperties("Photo"));
					
				}
				else
				{
					var obj:Object=StaticIni.getIniObj(arr[index*2]);
					if(obj == null)
						return;
					(_itemMC["img"] as NxClip).source            =  ResourcesPath.getPropsURL(int(obj["Photo"])); 
				}
				if(group == _ui.itemGroup)
				{
					_itemMC["titleTxt2"].text =arr[index*2+1]+"";
				}
				else
				{
					var val	:int	=_haveCoutArr[index];
					var val1:int	=arr[index*2+1];
					if(_composeType == 2 && index == 0)
					{
						
						val+1>=val1?_itemMC["titleTxt2"].htmlStr =link_color(val+1+"/"+val1,2):_itemMC["txtCount"].htmlStr =link_color(val+1+"/"+val1,1);
					}
					else
					{
						val>=val1?_itemMC["titleTxt2"].htmlStr =link_color(val+"/"+val1,2):_itemMC["txtCount"].htmlStr =link_color(val+"/"+val1,1);
					}
					
				}
				_itemMC["date"] = arr[index*2];
			}
		}
		/**
		 * 清除一个玩家信息 
		 * @param index
		 * 
		 */		
		private function removeTeamGroup(index : int,group:NxViewGroup):void
		{
			var itemMC : MovieClip = group.getItemByIndex(index);
			if(itemMC != null)
			{
				(itemMC["img"] as NxClip).source = "";
				(itemMC["img"] as NxClip).clear();
				if(group==_ui.materialGroup)
				{
					itemMC["titleTxt2"].text ="";
				}
				else
				{
					_itemMC["txtCount"].text ="";
				}
				_itemMC["date"]=null;

			}
		}
		/**
		 * 清除所有队伍信息
		 * @param group
		 * 
		 */		
		private function clearTeamGroup(group:NxViewGroup):void
		{
			for(var i : int = 0; i < group.row*group.col; i ++)
			{
				removeTeamGroup(i,group);
			}
		}
		/**
		 *检查所有材料的数量 
		 * 
		 */		
		private function searchALL():void
		{
			_haveCoutArr=[];
			var amout:int;
			for(var i:int=0;i<_materialCount;i++)
			{
				amout = searchItem(_materialArr[i*2],1);
				_haveCoutArr.push(amout);
			}
		}
		/**
		 * 检查仓库背包中物品的数量 
		 * @param id 物品ConfigID
		 * @param type 返回类型参数
		 * @return   type= 1物品数量 /type= 2物品(返回等级最高的)
		 * 
		 */		
		public function searchItem(ConfigId:int,type:int):*
		{
			var packVo:View		=	_modle.scene.GetView(View.VIEWPORT_TOOL);
			//背包数量
			var itemArr:Array =[];  //存放该configid 所有物品数组
			var packCount:int=0;
			if(packVo !=null)
			{
				for (var property:String in packVo.Properties) 
				{
					if(int(property))
					{
						if(packVo.CheckProperties(property))
						{
							var item:ViewObject=packVo.GetPropertiesEx(property);
							if(item.GetPropertiesEx("ConfigID")==ConfigId)
							{
								
								if(item.CheckProperties("Amount"))
								{
									packCount+=item.GetPropertiesEx("Amount");
								}
								else
								{
									packCount++;
								}
								if(type == 2)
								{
									itemArr.push(item);
								}
							}
						}
						
					}
				}
			}
	
			var depotVo:View	=	_modle.scene.GetView(View.VIEWPORT_STORE);
			//仓库数量
			var depotCount:int=0;
			for(var property1:String in depotVo.Properties)
			{
				if(int(property1))
				{
					if(depotVo.CheckProperties(property1))
					{
						var item1:ViewObject=depotVo.GetPropertiesEx(property1);
						if(item1.GetPropertiesEx("ConfigID")==ConfigId)
						{
//							if(item1.CheckProperties("EquipType")) //装备就不搜仓库
//								break;
							if(item1.CheckProperties("Amount"))
							{
								depotCount+=item1.GetPropertiesEx("Amount");
								
							}
							else
							{
								depotCount++;
							}
							if(type == 2)
							{
								itemArr.push(item1);
							}
						}
					}
				}
			}
		   if(type==1)
		   {
			   return packCount+depotCount;	
		   }
		   else
		   {
			   return checkLv(itemArr);
		   }
		}
		/**
		 *超找改ConfigID 等级最高的物品 
		 * @param arr
		 * @return 
		 * 
		 */		
		private function checkLv(arr:Array):*
		{
			var lv:int=0;
			var maxIndex:int=0;
			for(var i:int=0;i<arr.length;i++)
			{
				if(arr[i].GetPropertiesEx("IntensityLevel")>lv)
				{
					lv =arr[i].GetPropertiesEx("IntensityLevel");
					maxIndex =i;
				}
			}
			return arr[maxIndex];
		}
		/**
		 * 检查背包和仓库中装备（强化等级）的真实数量 
		 * @param item 装备
		 * @return  该强化等级的数量
		 * 
		 */		
		public function searchItem1(itemobj:*):int
		{
			if(!itemobj.CheckProperties("EquipType")) //不是装备不查找
				return -1;
			var ConfigId:int =itemobj.GetPropertiesEx("ConfigID");
			var lv:int =itemobj.GetPropertiesEx("IntensityLevel");
			var packVo:View		=	_modle.scene.GetView(View.VIEWPORT_TOOL);
			//背包数量
			var packCount:int=0;
			if(packVo !=null)
			{
				for (var property:String in packVo.Properties) 
				{
					if(int(property))
					{
						if(packVo.CheckProperties(property))
						{
							var item:ViewObject=packVo.GetPropertiesEx(property);
							if(item.GetPropertiesEx("ConfigID")==ConfigId)
							{
								if(item.GetPropertiesEx("IntensityLevel") ==lv)
								{
									packCount++;
								}
				
							}
						}
						
					}
				}
			}
			
			var depotVo:View	=	_modle.scene.GetView(View.VIEWPORT_STORE);
			//仓库数量
			var depotCount:int=0;
			for(var property1:String in depotVo.Properties)
			{
				if(int(property1))
				{
					if(depotVo.CheckProperties(property1))
					{
						var item1:ViewObject=depotVo.GetPropertiesEx(property1);
						if(item1.GetPropertiesEx("ConfigID")==ConfigId)
						{
							if(item1.GetPropertiesEx("IntensityLevel") ==lv)
							{
								depotCount++;
							}
				
						}
					}
				}
			}
			return packCount+depotCount;	

		}
		/**
		 * 检测材料够不 
		 * @return 
		 * 
		 */		
		private function checkIsCan():Boolean
		{
			var i:int;
			_composeType == 2?i=1:i=0;
			for(i;i<_materialCount;i++)
			{
				if(_haveCoutArr[i]<int(_materialArr[i*2+1]))
				{
					return false;
				}
			}
			return true;
		}
		
		/**
		 * 发放开始制造消息 
		 * @param obj  制造的物品
		 * @param type 类型  1 是点击背包制作  2点击装备
		 * @param viewtype  视图ID
		 * @param MsgType	1 普通制作 2立即完成
		 */		
		public function onSendCompose(obj:ViewObject,type:int,viewtype:int,key:int,MsgType:int):void
		{

			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_COMPOSE_ITEM,type,obj.GetPropertiesEx("ConfigID"),viewtype,key,MsgType);
		}
		private function link_color(str:String,color:int):String
		{
			if(color < 1 || color >8)
			{
				color	=	1;
			}
			return ("<font color ='"+COLOR_TABLE[color]+str+"</font>");
		}
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		private function onClose(e:NxWindowEvent):void
		{
			_ui.hide();
		}
		private function onCompose(e:MouseEvent):void
		{
			_state ==0?onSendCompose(_viewObj,_type,_vieType,int(_viewObj.ObjKey),1):onSendCompose(_viewObj,_type,_vieType,_state,1);
			
		}
		
		private function onClick( evt : MouseEvent ):void
		{
			var MC:MovieClip	=	evt.currentTarget as MovieClip;
			var obj:Object		=	MC.date;
			
			if(obj !=null)
			{
				if(obj is ViewObject)
				{
					return;
				}
			}
			if(obj != null && obj.ItemType == GameDefine.ItemType_Material)
			{
				var fbId : String = obj["FBID"];
				var sceneId : String = obj["TargetScene"];
				DuplicatesLogic.getInstance().FBSceneID(sceneId,DuplicatesLogic.COLL_FB);
				DuplicatesLogic.getInstance().showInfo( fbId );
			}
		}
		private function onClickCompleHandle(e:MouseEvent):void
		{
			AlertUI.showAlert("",StaticIni.getSwfLangStrVar("UIString_1000486",[_needMagicCoin]),AlertUI.YES|AlertUI.NO,clickBack)
//			_state ==0?onSendCompose(_viewObj,_type,_vieType,int(_viewObj.ObjKey),2):onSendCompose(_viewObj,_type,_vieType,_state,2);
			
		}
		private function clickBack(type:int, str:String, clickData:*):void
		{
			if(AlertUI.YES == type)//确定立即制作
			{
				_state ==0?onSendCompose(_viewObj,_type,_vieType,int(_viewObj.ObjKey),2):onSendCompose(_viewObj,_type,_vieType,_state,2);
			}
		}
		private function onshow(e:MouseEvent):void
		{
			//需要多少元宝
			var str:String =StaticIni.getSwfLangStrVar("UIString_1000486",[_needMagicCoin]);
			TipsMainLogic.instance.showString(str);
		
			
		}
		/**
		 *请求制作消耗 
		 * 
		 */		
		private function sendConsumeMsg():void
		{
			var pos:int=_state ==0?int(_viewObj.ObjKey):_state;
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_OPEN_SCROLL_PRODUCITON_UI,_type,_viewObj.GetPropertiesEx("ConfigID"),_vieType,pos);
		}
		/**
		 *获取制作结果成功 
		 * @param e
		 * 
		 */		
		private function getSuccees(e:CustomMsgEvent):void
		{
			setTimeout(update,500);
			_ui.succsemc.visible=true;
			_ui.succsemc.gotoAndPlay(1);
			_ui.succsemc.addFrameScript(_ui.succsemc.totalFrames-1,function ():void
			{
				_ui.hide();
				_ui.succsemc.stop();
			
			})
			//播放动画
			_ui.startBtn.enabled=_ui.compleBtn.enabled=false;
		}
		private function update():void
		{
			searchALL();
			updataItem(_materialArr,_ui.materialGroup);
		}
		/**
		 *获取制作立即完成的消耗 
		 * @param e
		 * 
		 */		
		private function getConsume(e:CustomMsgEvent):void
		{
			_needMagicCoin = e.pList[0];
		}
		private function onShowTips(e:MouseEvent):void
		{
			var MC:MovieClip	=	e.currentTarget as MovieClip;
			var obj:Object		=	MC.date;
			if(obj == null)
				return;
			TipsMainLogic.instance.showMain(obj,false);
			if(_resectMc)
			{
				_resectMc.gotoAndStop(1)
			}
			MC.gotoAndStop(2);
			_resectMc =MC;
		}
		
		public function get needMagicCoin():int
		{
			return _needMagicCoin;
		}
		private function onHideTips(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
			if(_resectMc)
				_resectMc.gotoAndStop(1);
		}
	}
}